﻿// Open Source: MIT License (http://www.opensource.org/licenses/mit-license.php)
// Project: YoProgramo.NET (http://code.google.com/p/yoprogramo/)
// Autor: Fabian Figueredo (info@fabianfigueredo.com.ar)
// Blog: blog.yoprogramo.net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.Windsor;
using NHibernate;
using YoProgramo.Domain.Core.Interfaces;

namespace YoProgramo.Infrastructure.Data.Repositories
{
    public abstract class RepositoryBase<TIType, TEntity , Tid> {
        protected IDao<TEntity, TIType, Tid> dao;

        protected RepositoryBase() {
            dao = new DAOFactory<TEntity, TIType, Tid>().Get();
        }

        /// <summary>
        /// Constructor used for unit test and DI.        
        /// </summary>
        /// <param name="daoObj">DAO object Class</param>
        protected RepositoryBase(IDao<TEntity, TIType, Tid> daoObj)
        {
            dao = daoObj;
        }

        public virtual TIType Get(Tid id) {
            return (TIType) ((object)dao.GetById(id));//           
        }

        public virtual IList<TIType> GetAll() {
            return dao.GetAll();
        }


        public virtual void Delete(TIType entity) {
            dao.Delete(entity);
        }

        /// <summary>
        /// Although SaveOrUpdate _can_ be invoked to update an object with an assigned Id, you are 
        /// hereby forced instead to use Save/Update for better clarity.
        /// </summary>
        public virtual TIType SaveOrUpdate(TIType entity) {
            dao.SaveOrUpdate(entity);
            return entity;
        }


    }
}